﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace BussinessLogic.Helpers
{
    public static class EmailHelper
    {
        public static void SendConfirmationEmail(string emailTo, string displayName, Guid? activationHash)
        {
            string subject = "Actvation Email";
            string server = "localhost";
            string activationUrl = String.Format("{0}/account/verify?email={1}&key={2}", server, emailTo, activationHash);

            string body = string.Format(@"Thanks for signing up!
Your account has been created, you can login with the following credentials after you have activated your account by pressing the url below.

http://{0}", activationUrl);


            SendEmail(emailTo, displayName, subject, body);
        }

        public static void SendCreatAdminEmail(string emailTo, string displayName, Guid? activationHash)
        {
            string subject = "Actvation Email";
            string server = "localhost";
            string activationUrl = String.Format("{0}/account/AdminRegister?email={1}&key={2}", server, emailTo, activationHash);

            string body = string.Format(@"Thanks for signing up!
Your account has been created, you can login with the following credentials after you have activated your account by pressing the url below.

http://{0}", activationUrl);


            SendEmail(emailTo, displayName, subject, body);
        }

        public static void SendChangePasswordEmail(string emailTo, string displayName, Guid? resetHash)
        {
            string subject = "Password Reset";
            string server = "localhost";
            string resetUrl = String.Format("{0}/account/ChangePassword?email={1}&key={2}", server, emailTo, resetHash);

            string body = string.Format(@"A password change for the account with the e-mail address {0} has been requested.
If you are trying to reset your password, please visit the URL below.

If you did not use the 'Forgot password?' form, please ignore this e-mail and your password will remain unchanged.

http://{1}
", emailTo, resetUrl);

            SendEmail(emailTo, displayName, subject, body);
        }

        private static void SendEmail(string emailTo, string displayName, string subject, string body)
        {
            MailAddress fromAddress = new MailAddress("ubbonlinetherapy@gmail.com", "On-Line Therapy");
            MailAddress toAddress = new MailAddress(emailTo, displayName);

            string fromPassword = "onlinetherapy";

            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
            };

            using (MailMessage message = new MailMessage(fromAddress, toAddress)
            {
                Subject = subject,
                Body = body
            })
            {
                smtp.Send(message);
            }

        }
    }
}
